package com.yp.demo.web;

import io.swagger.annotations.ApiOperation;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

/**
 * Created by CrazyMouse on 2017/7/14.
 */
@RestController
public class LoginController implements ErrorController {

    private static final String PATH = "error";

    @Override
    public String getErrorPath() {
        return PATH;
    }

    @ApiIgnore
    @RequestMapping(value = PATH)
    public Object error(HttpServletRequest request, HttpServletResponse response) {
        if (response.getStatus() == 401){
            try {
                System.err.print(request.getQueryString());
                if (request.getQueryString() !=null && request.getQueryString().contains("redirect_uri=http")){
                    response.sendRedirect("http://localhost:3000/login");
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        HashMap<String, String> json = new HashMap<>();
        json.put("status",response.getStatus()+"");
        return json;
    }

    @ApiOperation("登录")
    @PostMapping(value = "/login", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public void login(@RequestParam String username, @RequestParam String password) {

    }

    @ApiOperation("登出")
    @PostMapping("/logout")
    public void logout() {

    }
}
